//
//  WelcomeViewController.swift
//  MyNavigationbar
//
//  用户指导界面
//  Created by teleframe on 15/8/24.
//  Copyright (c) 2015年 tf. All rights reserved.
//

import UIKit

class WelcomeViewController: UIViewController,UIScrollViewDelegate {
    
    var scrollView = UIScrollView()
    var pageControl = UIPageControl()
    var startButton = UIButton()
    var viewWidth : CGFloat!
    var viewHeight : CGFloat!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        viewWidth = self.view.frame.width
        viewHeight = self.view.frame.height
        pageControl.center = CGPointMake(viewWidth/2, viewHeight-30)
        pageControl.currentPageIndicatorTintColor = UIColor.redColor()
        pageControl.pageIndicatorTintColor = UIColor.blueColor()
        pageControl.numberOfPages = 3
        pageControl.addTarget(self, action: "scrollViewDidEndDecelerating", forControlEvents: UIControlEvents.ValueChanged)
        scrollView.frame = self.view.frame
        scrollView.contentSize = CGSizeMake(3 * viewWidth,0)
        scrollView.pagingEnabled = true
        scrollView.bounces = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        self.view.addSubview(scrollView)
        
        for (var i=0 ; i < 3 ; i++){
            
            let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight))
            
            imageView.image = UIImage(named: "welcome\(i+1)")
            var frame = imageView.frame
            frame.origin.x = CGFloat(i)*frame.size.width
            imageView.frame = frame
            scrollView.addSubview(imageView)
        }
        addButton()
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        var index = Int(scrollView.contentOffset.x/self.view.frame.width)
        pageControl.currentPage = index
        if(index==2){
            UIView.animateWithDuration(0.5, delay: 0.1, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                self.startButton.frame = CGRectMake(0.5*self.viewWidth-50, self.viewHeight-100, 100, 40)
                self.startButton.alpha = 1
                
                }, completion: nil)
        }else{
            self.startButton.frame = CGRectMake(0.5*viewWidth-50, viewHeight, 100, 40)
            self.startButton.alpha = 0
        }
    }
    
    func addButton(){
        
        self.startButton.frame = CGRectMake(0.5*viewWidth-50, viewHeight, 100, 40)
        self.startButton.setTitle("开始", forState: UIControlState.Normal)
        self.startButton.titleLabel?.font = UIFont.systemFontOfSize(20)
        self.startButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        self.startButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)
        
        self.startButton.layer.cornerRadius = 3
        self.startButton.layer.masksToBounds = true
        self.startButton.layer.borderWidth = 1
        self.startButton.layer.borderColor = UIColor.blueColor().CGColor
        self.startButton.alpha = 1
        self.startButton.addTarget(self, action:"buttonClick1", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(self.startButton)
        
    }
    
    func buttonClick1()
    {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("mainTab") as! MyTabBarController
        self.presentViewController(vc, animated: true, completion: nil)
    }
    
    
    
    
    
    
    
    
    
}
